package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.Options;

import java.util.List;

/**
 *
 * @author wenqin 2017-07-12 16:27
 */

public class MainItemAdapter extends RecyclerView.Adapter<MainItemAdapter.Vh> {

    private List<Options> mOptionsList;
    private Context mContext;
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;

    public MainItemAdapter(Context context, List<Options> optionsList) {
        mOptionsList = optionsList;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.adapter_item, parent, false));
    }

    @Override
    public void onBindViewHolder(Vh holder, int position) {
        Glide.with(mContext).load(mOptionsList.get(position).getIcon()).error(R.mipmap.img_default).into(holder.mItemIcon);
        holder.mItemText.setText(mOptionsList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return mOptionsList == null ? 0 : mOptionsList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        ImageView mItemIcon;
        TextView mItemText;

        public Vh(View itemView) {
            super(itemView);
            mItemIcon = itemView.findViewById(R.id.item_icon);
            mItemText = itemView.findViewById(R.id.item_text);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
